﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;

namespace Yule8xWxToolkit.Other
{
    public partial class WHAutoRegist : System.Web.UI.Page
    {
        protected string ah;
        protected FormParams formParams;
        protected void Page_Load(object sender, EventArgs e)
        {
            ah = RequestUtil.ParamsString("ah");
            formParams = new Other.FormParams()
            {
                uid1 = RequestUtil.GetInteger("uid1"),
                uid2 = RequestUtil.GetInteger("uid2"),
                regtime1 = RequestUtil.GetString("regtime1"),
                regtime2 = RequestUtil.GetString("regtime2"),
                exp1 = RequestUtil.GetInteger("exp1"),
                exp2 = RequestUtil.GetInteger("exp2"),
                mode = RequestUtil.GetString("mode"),
                upfield = RequestUtil.GetString("upfield"),
                gamelogincount1 = RequestUtil.GetInteger("gamelogincount1"),
                gamelogincount2 = RequestUtil.GetInteger("gamelogincount2"),
                logintimes1 = RequestUtil.GetInteger("logintimes1"),
                logintimes2 = RequestUtil.GetInteger("logintimes2"),
                playtimes1 = RequestUtil.GetInteger("playtimes1"),
                playtimes2 = RequestUtil.GetInteger("playtimes2"),
            };
            if (formParams.uid1 > 0 && formParams.uid2 > 0)
            {
                doUpdate();
            }
        }
        void doUpdate()
        {
            string[] upfields = string.IsNullOrEmpty(formParams.upfield) ? null : formParams.upfield.Split(',');
            DateTime regTime1 = DateTime.Now;
            double seconds = 0;
            if (!string.IsNullOrEmpty(formParams.regtime1) && !string.IsNullOrEmpty(formParams.regtime1) && formParams.regtime1.Contains(":"))
            {
                regTime1 = DateTime.Parse(formParams.regtime1);
                TimeSpan tspan = DateTime.Parse(formParams.regtime2) - regTime1;
                seconds = tspan.TotalSeconds;
            }
            // 随机昵称，这个从之前我们准备的那个名字库里取，取出来的名字，后面要加8位大写字符，字符由1-9，以及英文字符A-F组成，或根据标识ID，生成 "游客"+标识ID
            // 用户账号,如果用户昵称是名字库里取的，那生成的用户账号是E001A2EA_1AC2_4187_BB08_3F724F1，这样的结构，这些数据随便生成，按照这个规范就行
            string[] nicknames = RuntimeDataUtil.GetNickNameText().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            string sqlstmt, nickname, strdict = "ABCDEF0123456789", rnd8;
            int nicknamescount = nicknames.Length - 1, dictlen = strdict.Length, playtimes, logincount;
            Random rnd = new Random();
            IList<string> fieldsAcc = new List<string>();
            IList<string> fieldsTrea = new List<string>();
            bool isRun = formParams.mode == "run";
            if (!isRun)
            {
                Response.Write("<textarea cols='120' rows='30' style='width:96%'>");
            }
            for (int i = formParams.uid1; i <= formParams.uid2; i++)
            {
                fieldsAcc.Clear();
                fieldsTrea.Clear();
                if (null == upfields)
                {
                    break;
                }
                if (upfields.Contains("nickname"))
                {
                    if (rnd.Next(100) % 3 == 0)
                    {
                        nickname = nicknames[rnd.Next(nicknamescount)];
                        nickname = nickname.Substring(nickname.IndexOf(' ') + 1);
                        if (nickname.Length < 24)
                        {
                            rnd8 = "";
                            for (int i8 = 0; i8 <= 8; i8++)
                            {
                                rnd8 += strdict[rnd.Next(dictlen)];
                            }
                            nickname = nickname + rnd8;
                        }
                        fieldsAcc.Add(string.Format("accounts='{0}'", Guid.NewGuid().ToString().ToUpper().Replace('-', '_').Substring(0, 31)));
                    }
                    else
                    {
                        nickname = "游客" + i;
                        fieldsAcc.Add(string.Format("accounts='{0}'", nickname));
                        fieldsAcc.Add(string.Format("RegAccounts='{0}'", nickname));

                    }
                    fieldsAcc.Add(string.Format("Nickname='{0}'", nickname));
                }
                if (upfields.Contains("regtime") && seconds > 0)
                {
                    fieldsAcc.Add(string.Format("RegisterDate='{0}'", regTime1.AddSeconds(rnd.NextDouble() * seconds).ToString("yyyy-MM-dd HH:mm:ss")));
                }
                if (upfields.Contains("sex"))
                {
                    fieldsAcc.Add(string.Format("Gender={0}", rnd.Next(2)));
                }
                if (upfields.Contains("exp") && formParams.exp2 > 0)
                {
                    fieldsAcc.Add(string.Format("experience={0}", rnd.Next(formParams.exp1, formParams.exp2)));
                }
                if (upfields.Contains("logincount"))
                {
                    logincount = rnd.Next(formParams.gamelogincount1, formParams.gamelogincount2);
                    fieldsAcc.Add(string.Format("GameLogonTimes={0}", logincount));
                    fieldsTrea.Add(string.Format("AllLogonTimes={0}", logincount));
                }
                // update游戏币管理里的游戏时长和在线时长，所有的游戏时长update数据0-400，然后在线时长在生成的这个数据上，随机增加300-600，登录次数所有等于0的 改成1-3次
                if (upfields.Contains("playtimes"))
                {
                    playtimes = rnd.Next(formParams.playtimes1, formParams.playtimes2);
                    fieldsTrea.Add(string.Format("PlayTimeCount={0}", playtimes));
                    if (upfields.Contains("logintimes"))
                    {
                        fieldsTrea.Add(string.Format("OnLineTimeCount={0}", playtimes + rnd.Next(formParams.logintimes1, formParams.logintimes2)));
                    }
                }

                if (fieldsTrea.Count > 0)
                {
                    sqlstmt = string.Format("UPDATE RYTreasureDB.dbo.GameScoreInfo SET {0} where UserID={1};", string.Join(",", fieldsTrea), i);
                    if (isRun)
                    {
                        DbFactory.Init(AppInfo.con_wanghu).ExecuteNonQuery(sqlstmt);
                    }
                    else
                    {
                        Response.Write(sqlstmt + Environment.NewLine);
                    }
                }
                if (fieldsAcc.Count > 0)
                {
                    sqlstmt = string.Format("UPDATE RYAccountsDB.dbo.AccountsInfo SET {0} where UserID={1};", string.Join(",", fieldsAcc), i);
                    if (isRun)
                    {
                        DbFactory.Init(AppInfo.con_wanghu).ExecuteNonQuery(sqlstmt);
                    }
                    else
                    {
                        Response.Write(sqlstmt + Environment.NewLine);
                    }
                }
            }
            if (!isRun)
            {
                Response.Write("</textarea>");
            }
            else
            {
                Response.Write("更新完成!");
            }

        }
    }
    public class FormParams
    {
        public int uid1 { get; set; }
        public int uid2 { get; set; }
        public string regtime1 { get; set; }
        public string regtime2 { get; set; }
        public int exp1 { get; set; }
        public int exp2 { get; set; }
        public string mode { get; set; }
        public string upfield { get; set; }
        public int gamelogincount1 { get; set; }
        public int gamelogincount2 { get; set; }
        public int logintimes1 { get; set; }
        public int logintimes2 { get; set; }
        public int playtimes1 { get; set; }
        public int playtimes2 { get; set; }
    }
}